package com.yunbiao.ybbusstopplate.play_sync;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.GsonUtils;
//import com.janev.chongqing_bus_app.db.DaoManager;
//import com.janev.chongqing_bus_app.db.Material;
//import com.janev.chongqing_bus_app.system.Path;
//import com.janev.chongqing_bus_app.utils.BytesUtils;
//import com.janev.chongqing_bus_app.utils.ProgramLoader;
//import com.janev.chongqing_bus_app.utils.ScreenPikerUtils;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class SocketUtil {

    public static String getAction(String jsonString) {
        JSONObject jsonObject = (JSONObject) JSONObject.parse(jsonString);
        return jsonObject.getString("action");
    }

    public static String getPlayData(String filePath, int position, int viewPosition) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("action", "1");
        jsonObject.put("viewPosition", viewPosition);
        jsonObject.put("filePath", filePath);
        jsonObject.put("position", position);
        return jsonObject.toJSONString();
    }

    /**
     * 进度相差 不大于100毫秒
     *
     * @param first
     * @param second
     */
    public static boolean isSamePosition(int first, int second) {

        boolean isSamePosition = true;
        int p = first - second;
        if (p > 100 || p < -100) {
            isSamePosition = false;
        }

        return isSamePosition;
    }

//    /**
//     *
//     * @param material
//     * @param fileName
//     * @param data
//     * @param flag
//     * @return
//     */
//    public static String getFileData(String fileName, byte[] data,int flag,Material material ) {
//
//        String hexString = BytesUtils.bytesToHex(data);
//        ProgramFile programFile = new ProgramFile("0",Path.getMaterialPath(material),fileName,hexString,flag,material);
//        return GsonUtils.toJson(programFile);
////        JSONObject jsonObject = new JSONObject();
////        jsonObject.put("action", "0");
////        jsonObject.put("programId", material.getProgramId());
////        jsonObject.put("materialId", material.getId());
////        jsonObject.put("materialPath", Path.getMaterialPath(material));
////        jsonObject.put("fileName", fileName);
////        jsonObject.put("size", material.getSize());
////        jsonObject.put("data", hexString);
////        jsonObject.put("flag", flag);
////        return jsonObject.toJSONString();
//    }

//    public static String getPlayData(Material material, int position) {
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("action", "1");
//        jsonObject.put("programId", material.getProgramId());
//        jsonObject.put("materialId", material.getId());
//        jsonObject.put("position", position);
//        return jsonObject.toJSONString();
//    }

    /**
     * 刷新数据库
     * @param material
     */
//    public static void setMaterial(Material material) {
//        material.setPlayTotalTimes(0);
//        material.setPlayTotalDuration(0);
//        List<Material> materials = new ArrayList<>();
//        materials.add(material);
//        DaoManager.get().addOrUpdateMaterials(materials);
//    }

    /**
     * 是否全部下载完成
     * @return
     */
//    public static boolean allComplete(){
//        List<Material> materialList = ProgramLoader.loadDownloadList();
//        int i = 0;
//        for (Material material : materialList) {
//            String materialPath = Path.getMaterialPath(material);
//            if(FileUtils.isFileExists(materialPath)){
//                i += 1;
//            }
//        }
//        return i == materialList.size();
//    }
//
//    public static void d(String tag,String log) {
//        Log.d(tag, log);
//        if (ScreenPikerUtils.isMasterScreen()) {
//            ServerSocket.getInstance().inputLog(log);
//        } else {
//            ClientSocket.getInstance().inputLog(log);
//        }
//    }

}
